﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TernimalPanel : MonoBehaviour {
    private static TernimalPanel m_Ins;
    public static TernimalPanel Ins {
        get { return m_Ins; }
    }
    private string terminalText = string.Empty;
    [SerializeField]
    private Text contentText;
    [SerializeField]
    private InputField inputField;
    [SerializeField]
    private ScrollRect scrollRect;

    private void Awake() {
        m_Ins = this;
    }

    // Use this for initialization
    void Start () {
        inputField.Select();
	}
	
	// Update is called once per frame
	void Update () {
        contentText.text = terminalText;
	}

    public void AppendText(string text) {
        terminalText += text;
        StartCoroutine(Scroll2Bottom(0));
    }

    public void AppendLine(string text) {
        terminalText += "\n" + text;
        StartCoroutine(Scroll2Bottom(0));
    }

    public void OnEndEdit() {
        GameMgr.Ins.OnCommandInput(inputField.text);
        inputField.text = "";
        inputField.Select();
        inputField.ActivateInputField();
    }

    IEnumerator Scroll2Bottom(float x) {
        yield return new WaitForEndOfFrame();
        scrollRect.verticalNormalizedPosition = x;
    }
}
